package com.gs.netty.websocket.remoting;



import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;


@Data
public class WebSocketInvocation implements Invocation, Serializable {
    /**
     * 请求id生成器
     */
    private static final AtomicInteger REQUEST_ID;

    static {
        REQUEST_ID = new AtomicInteger(1);
    }


    /**
     * 请求Id
     */
    private Integer requestId;
    /**
     * 状态码
     */
    private Integer status=0;
    /**
     * 信息类型
     */
    private int msgType =0;
    /**
     * 请求指令编码
     */
    private String command;

    /**
     * 发送数据
     */
    private String payload;


    public WebSocketInvocation(String command, String payload) {
        this.command = command;
        this.payload = payload;
        this.requestId = newId();
    }

    private static int newId() {
        return REQUEST_ID.getAndIncrement();
    }

    public WebSocketInvocation() {
    }


    @Override
    public void setCommand(String command) {
        this.command = command;
    }

    @Override
    public Integer getRequestId() {
        return requestId;
    }



    public void setMsgType(Integer msgType) {
        this.msgType = msgType&1;
    }

    public void setRequestId(Integer requestId) {
        this.requestId = requestId;
    }

    @Override
    public String getCommand() {
        return command;
    }

    public boolean isRequest(){
        return msgType == 0;
    }





    @Override
    public Invocation create() {
        WebSocketInvocation webSocketInvocation = new WebSocketInvocation();
        webSocketInvocation.setRequestId(this.requestId);
        webSocketInvocation.setMsgType((~this.msgType& 1));
        return webSocketInvocation;
    }

    public Integer getStatus() {
        return status;
    }

    public void setOK(Integer status) {
        this.status = status-1;
    }

    public String getPayload() {
        return payload;
    }

    public void setPayload(String payload) {
        this.payload = payload;
    }
}


